package com.example.lib_cache.viewmodel;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.alibaba.fastjson.JSONObject;
import com.example.lib_cache.model.Navigation;
import com.example.lib_cache.model.Order;
import com.example.lib_cache.model.User;
import com.example.lib_cache.network.exception.ApiException;
import com.example.lib_cache.network.exception.ErrorConsumer;
import com.example.lib_cache.network.response.ResponseResultTransformer;
import com.example.lib_cache.service.ApiService;
import com.example.lib_common.util.LiveDataManager;
import com.example.lib_common.util.MileageUtils;
import com.example.lib_common.util.TimeUtils;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class UserFragmentViewModel extends ViewModel {

    private final static String TAG = UserFragmentViewModel.class.getSimpleName();

    private MutableLiveData<User> userMutableLiveData = new MutableLiveData<>();

    private MutableLiveData<String> tokenMutableLiveData;

    private MutableLiveData<Order> orderMutableLiveData = new MutableLiveData<>();

    Navigation settleNavigation = null;
    String distance = "";
    String duration = "";
    String distanceUnit = "";
    MutableLiveData<Boolean> isTransform = new MutableLiveData<>();

    public String getDistance() {
        return distance;
    }

    public String getDuration() {
        return duration;
    }

    public String getDistanceUnit() {
        return distanceUnit;
    }

    public MutableLiveData<Boolean> getIsTransform() {
        return isTransform;
    }

    public void setIsTransform(Boolean isTransform) {
        this.isTransform.postValue(isTransform);
    }

    public MutableLiveData<Order> getOrderMutableLiveData() {
        return orderMutableLiveData;
    }

    public void setOrderMutableLiveData(Order order) {
        this.orderMutableLiveData.postValue(order);
    }

    public MutableLiveData<User> getUserMutableLiveData() {
        return userMutableLiveData;
    }

    public void setUserMutableLiveData(User user) {
        userMutableLiveData.postValue(user);
    }

    public void loadingUserData(ApiService apiService ,Context context){
        apiService.getUserInfoByToken()
                .compose(ResponseResultTransformer.<User>obtain())
                .subscribe(new Consumer<User>() {
                    @Override
                    public void accept(User user) throws Exception {
                        Log.d(TAG,"访问成功"+ user.getAccount()+"  "+user.getNickname());
                        setUserMutableLiveData(user);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"访问失败");
                        Log.d(TAG,ex.getErrorMsg());
                    }
                });
    }

    public void requestOrderStatus(ApiService apiService,Context context){
        apiService.getSendingOrderStatus()
                .compose(ResponseResultTransformer.<Order>obtain())
                .subscribe(new Consumer<Order>() {
                    @Override
                    public void accept(Order order) throws Exception {
                        Log.d(TAG,"访问成功");
                        setOrderMutableLiveData(order);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"访问失败");
                        Log.d(TAG,ex.getCode());
                        Log.d(TAG,ex.getErrorMsg());
                    }
                });
    }

    public void saveData(){

        MutableLiveData<String> liveData =
                LiveDataManager.getInstance().with("nickname",String.class);
        liveData.setValue(userMutableLiveData.getValue().getNickname());

        MutableLiveData<String> liveData1 =
                LiveDataManager.getInstance().with("phone",String.class);
        liveData1.setValue(userMutableLiveData.getValue().getPhoneNumber());
    }

    public void logout(){
        tokenMutableLiveData = LiveDataManager
                .getInstance().with("cctoken",String.class);
        tokenMutableLiveData.setValue("null");
    }

    public void transformTime(String lastTime,String thisTime) {
        duration = TimeUtils.TimeCompareToHour(lastTime,thisTime);
        settleNavigation =  LiveDataManager
                .getInstance().with("settleNav", Navigation.class).getValue();
        distance = MileageUtils.meterToKilometersWithoutUnit(
                settleNavigation.getDistance());
        if(settleNavigation.getDistance()>1000){
            distanceUnit = "公里";
        }else {
            distanceUnit = "米";
        }
        setIsTransform(true);
    }
}
